package com.ruoyi.web.controller.chargepile;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.chargepile.domain.ChargePile;
import com.ruoyi.chargepile.domain.ChargeRecords;
import com.ruoyi.chargepile.domain.Orders;
import com.ruoyi.chargepile.domain.dto.ChargeRecordsAddDto;
import com.ruoyi.chargepile.domain.dto.ChargeRecordsDto;
import com.ruoyi.chargepile.domain.enums.ChargePileStatusEnum;
import com.ruoyi.chargepile.domain.enums.ChargeRecordsStatusEnum;
import com.ruoyi.chargepile.domain.enums.RolesEnum;
import com.ruoyi.chargepile.domain.vo.ChargeRecordsVo;
import com.ruoyi.chargepile.service.ChargePileService;
import com.ruoyi.chargepile.service.ChargeRecordsService;
import com.ruoyi.chargepile.service.OrdersService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.util.DateRange;
import com.ruoyi.util.Pages;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * <p>
 * 充电记录表 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/charge-records")
public class ChargeRecordsController {

    @Autowired
    private ChargeRecordsService chargeRecordsService;

    @Autowired
    private ISysRoleService iSysRoleService;

    @PostMapping("/addChargeRecords")
    public AjaxResult addChargeRecords(@RequestBody ChargeRecordsAddDto chargeRecordsAddDto) {
        return chargeRecordsService.addChargeRecords(chargeRecordsAddDto);
    }

    @GetMapping("/listChargeRecordsVo")
    public AjaxResult listChargeRecordsVo(Pages pages, DateRange dateRange, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTimePay, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTimePay, ChargeRecordsDto chargeRecordsDto) {
        PageHelper.startPage(pages.getPageNum(), pages.getPageSize());
        int pageNum = pages.getPageNum();
        int num = (pages.getPageNum() - 1) * pages.getPageSize();
        int size = pages.getPageNum() * pages.getPageSize();
        pages.setPageNum(num);
        pages.setPageSize(size);
        DateRange dateRangePay = new DateRange();
        dateRangePay.setBeginTime(beginTimePay);
        dateRangePay.setEndTime(endTimePay);

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<SysRole> sysRoles = iSysRoleService.selectRolesByUserId(sysUser.getUserId());
        List<String> collectRoleKey = sysRoles.stream()
                .map(item -> item.getRoleKey())
                .collect(Collectors.toList());
        if (!collectRoleKey.contains(RolesEnum.ADMIN.getCode()) && !collectRoleKey.contains(RolesEnum.ADMINISTRATOR.getCode())){
            chargeRecordsDto.setUserId(Math.toIntExact(sysUser.getUserId()));
        }

        List<ChargeRecordsVo> sizeList = chargeRecordsService.listChargeRecordsVo(null, dateRange, dateRangePay, chargeRecordsDto);
        List<ChargeRecordsVo> list = chargeRecordsService.listChargeRecordsVo(pages, dateRange, dateRangePay, chargeRecordsDto);
        PageInfo<ChargeRecordsVo> chargeRecordsVoPageInfo = new PageInfo<>(list);
        chargeRecordsVoPageInfo.setTotal(sizeList.size());
        chargeRecordsVoPageInfo.setPages(pageNum);
        return AjaxResult.success(chargeRecordsVoPageInfo);
    }

}

